﻿using Simhopp.Common;

namespace DB_test
{
    class Program
    {
        static void Main()
        {
            MyDbConnection.SetConnectionString(@"Data Source=DS9;Initial Catalog=Diving;Integrated Security=True;MultipleActiveResultSets=True");

            // CreateCompetitionAndSave();
            //CreateCompetition2AndSave();
            // var Competitions = CompetitionMap.ReadAllCompetitionsFromDatabase();
            //CreateAndSaveUser();
            //CreateDiverWithTwoJumpsAndSave();
            // CreateCompetitionAndSave();
            var test = ReadCompetition.GetAllCompetitions();

            // CreateReadAndDeleteUser();
            // CreateReadAndDeleteDiver();
            // CreateReadAndDeleteCompetition();
        }

        private static void CreateReadAndDeleteUser()
        {
            CreateAndSaveUser();
            var users = UserMap.ReadAllUsersFromDataBase();
            UserMap.RemoveUserFromDataBase(users[0]);
            users = UserMap.ReadAllUsersFromDataBase();
        }

        private static void CreateAndSaveUser()
        {
            var user = new User
            {
                Name = "Judge1",
                UserName = "judge1",
                Age = 23,
                Gender = Genders.Female,
                Nationality = "Norway",
                SocialSecurityNumber = "19420823-1243"
            };
            UserMap.SaveUserToDatabase(user);
        }

        private static void CreateReadAndDeleteCompetition()
        {
            CreateCompetitionAndSave();
            var competitions = CompetitionMap.ReadAllCompetitionsFromDatabase();
            CompetitionMap.RemoveCompetitionFromDatabase(competitions[0]);
            competitions = CompetitionMap.ReadAllCompetitionsFromDatabase();
        }

        private static void CreateReadAndDeleteDiver()
        {
            CreateDiverWithTwoJumpsAndSave();
            var divers = DiverMap.ReadAllDiversFromDatabase();
            DiverMap.RemoveDiverFromDatabase(divers[0]);
            divers = DiverMap.ReadAllDiversFromDatabase();
        }

        private static void CreateCompetitionAndSave()
        {
            var competition = new Competition
            {
                Name = "Open 2015",
                City = "Kumla",
                Country = "Sweden",
                Date = "2015-02-22",
                Gender = Genders.Female,
            };
            competition.RemoveAllHeights();
            competition.AddHeight(Heights.OneMeter);
            competition.AddHeight(Heights.FiveMeters);
            competition.AddHeight(Heights.TenMeters);
            Competition.NumberOfJudges = 7;
            
            var diver = new Diver
            {
                Name = "Gunno Norbaggsdatter", 
                Gender = Genders.Female,
                Age = 25,
                Nationality = "Norway",
                SocialSecurityNumber = "19900112-2214"
            };
            var d1 = new Dive { KeyId = 22 };
            var d2 = new Dive { KeyId = 45 };
            var judge1 = new User
            {
                Name = "Judge One",
                UserName = "j1",
                Age = 44,
                Gender = Genders.Male,
                Nationality = "English",
                SocialSecurityNumber = "19440223-4412"
            };
            var judge2 = new User()
            {
                Name = "Domare2",
                UserName = "d2",
                Age = 73,
                Gender = Genders.Female,
                Nationality = "Sweden",
                SocialSecurityNumber = "19231213-6612"
            };
            var s1 = new Score
            {
                Judge = judge1,
                Value = 12.5f
            };
            var s2 = new Score
            {
                Judge = judge2,
                Value = 45f
            };
            d1.Scores.Add(s1);
            d2.Scores.Add(s2);
            diver.RemoveAllDives();
            diver.Dives.Add(d1);
            diver.Dives.Add(d2);
            competition.AddParticipant(diver);
            competition.AddJudge(judge1);
            competition.AddJudge(judge2);

            CompetitionMap.SaveCompetitionToDatabase(competition);
        }

        private static void CreateCompetition2AndSave()
        {
            var competition = new Competition
            {
                Name = "Open 2015",
                City = "Västerås",
                Country = "Sweden",
                Date = "2015-03-30",
                Gender = Genders.Male,
            };
            CompetitionMap.SaveCompetitionToDatabase(competition);
        }

        private static void CreateDiverWithTwoJumpsAndSave()
        {
            var diver = new Diver { Name = "D1" };
            var d1 = new Dive { KeyId = 22 };
            var d2 = new Dive { KeyId = 45 };
            var judge1 = new User
            {
                Name = "J1",
                UserName = "j1"
            };
            var judge2 = new User
            {
                Name = "J2",
                UserName = "j2"
            };
            var s1 = new Score
            {
                Judge = judge1,
                Value = 22.3f
            };
            var s2 = new Score
            {
                Judge = judge2,
                Value = 10f
            };
            d1.Scores.Add(s1);
            d2.Scores.Add(s2);
            diver.RemoveAllDives();
            diver.Dives.Add(d1);
            diver.Dives.Add(d2);
            DiverMap.SaveDiverToDatabase(diver);
        }
    }
}
